package com.ziyun.pop.modules.mall.service.impl;

import com.service.goods.api.SearchService;
import com.service.goods.constant.GoodsResult;
import com.service.goods.model.category.GoodsVO;
import com.service.goods.model.search.GoodsCacheSearchResultDto;
import com.service.goods.model.search.GoodsSearchParamDto;
import com.utility.JsonUtils;
import com.utility.New;
import com.ziyun.pop.modules.mall.dao.PurchaseGoodsDao;
import com.ziyun.pop.modules.mall.entity.PopGoodsConfigEntity;
import com.ziyun.pop.modules.mall.service.PurchaseGoodsService;
import com.ziyun.pop.modules.mall.vo.PurchaseGoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author gaosheng
 * @date 2018-12-25 10:44
 */
@Slf4j
@Service("purchaseGoodsService")
public class PurchaseGoodsServiceImpl implements PurchaseGoodsService {

    @Autowired
    private SearchService searchService;
    @Autowired
    private PurchaseGoodsDao purchaseGoodsDao;

    @Override
    public List<PurchaseGoodsVo> queryGoodsList() {
        log.info("===========================purchase goods search api start===========================");

//        List<Long> list = New.arrayList();
//        list.add(7776612L);
//        list.add(7776602L);
//        list.add(7776600L);
//        list.add(8253222L);

        List<PopGoodsConfigEntity> goodsList = purchaseGoodsDao.getPopGoodsConfig();
        List<Long> goodsIdList = goodsList.stream().map(PopGoodsConfigEntity::getGoodsId).collect(Collectors.toList());
        GoodsSearchParamDto goodsSearchParamDto = new GoodsSearchParamDto();
        goodsSearchParamDto.setGoodsIds(goodsIdList);
        goodsSearchParamDto.setSoure("popweb");
        GoodsResult<GoodsCacheSearchResultDto> goodsCacheSearchResultDtoGoodsResult = searchService.searchGoodsList(goodsSearchParamDto);
        GoodsCacheSearchResultDto goodsCacheSearchResultDto = goodsCacheSearchResultDtoGoodsResult.getData();
        List<GoodsVO> productSearchVo = goodsCacheSearchResultDto.getProductSearchVo();
        log.info("===========================purchase goods search api result:" + JsonUtils.object2String(productSearchVo));
        List<PurchaseGoodsVo> goods = New.arrayList(productSearchVo.size());
        for (GoodsVO goodsVO: productSearchVo){
            PurchaseGoodsVo purchaseGoodsVo = new PurchaseGoodsVo();
            purchaseGoodsVo.setGoodsId(goodsVO.getGoodsId());
            purchaseGoodsVo.setGoodsName(goodsVO.getGoodsName());
            purchaseGoodsVo.setGoodsType(goodsVO.getGoodsType().toString());
            purchaseGoodsVo.setDefaultImage(goodsVO.getDefaultImage());
            purchaseGoodsVo.setPopSinglePriceShow(goodsVO.getPopSinglePriceShow());
            purchaseGoodsVo.setSellerId(goodsVO.getSellerId());
            purchaseGoodsVo.setSellerName(goodsVO.getSellerName());
            goods.add(purchaseGoodsVo);
        }
        log.info("===========================purchase goods search api end===========================");
        return goods;
    }
}
